home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / ycbcr.c < prev   
C/C++ Source or Header  |  1991-12-16  |  4KB  |  159 lines

  1. float    ycbcrCoeffs[3] = { .299, .587, .114 };
  2. /* default coding range is CCIR Rec 601-1 with no headroom/footroom */
  3. unsigned long refBlackWhite[6] = { 0, 255, 128, 255, 128, 255 };
  4.  
  5. #define    LumaRed        ycbcrCoeffs[0]
  6. #define    LumaGreen    ycbcrCoeffs[1]
  7. #define    LumaBlue    ycbcrCoeffs[2]
  8.  
  9. long    eRtotal = 0;
  10. long    eGtotal = 0;
  11. long    eBtotal = 0;
  12. long    preveRtotal = 0;
  13. long    preveGtotal = 0;
  14. long    preveBtotal = 0;
  15. unsigned long AbseRtotal = 0;
  16. unsigned long AbseGtotal = 0;
  17. unsigned long AbseBtotal = 0;
  18. unsigned long eCodes = 0;
  19. unsigned long preveCodes = 0;
  20. unsigned long eBits = 0;
  21. unsigned long preveBits = 0;
  22.  
  23. static    void setupLumaTables();
  24. static int abs(int v) { return (v < 0 ? -v : v); }
  25. static double pct(int v,double range) { return (v*100. / range); }
  26.  
  27. float    D1, D2;
  28. float    D3, D4;
  29. float    D5, D6;
  30.  
  31. main(argc, argv)
  32.     char *argv[];
  33. {
  34.     int R, G, B;
  35.  
  36.     if (argc > 1) {
  37.     refBlackWhite[0] = 16;
  38.     refBlackWhite[1] = 235;
  39.     refBlackWhite[2] = 128;
  40.     refBlackWhite[3] = 240;
  41.     refBlackWhite[4] = 128;
  42.     refBlackWhite[5] = 240;
  43.     }
  44.     D3 = 2 - 2*LumaRed;
  45.     D4 = 2 - 2*LumaBlue;
  46.     D1 = 1. / D3;
  47.     D2 = 1. / D4;
  48.     D5 = D3*LumaRed / LumaGreen;
  49.     D6 = D4*LumaBlue / LumaGreen;
  50.     setupLumaTables();
  51.     for (R = 0; R < 256; R++) {
  52.     for (G = 0; G < 256; G++)
  53.         for (B = 0; B < 256; B++)
  54.         check(R, G, B);
  55.     printf("[%3u] c %u/%u b %u/%u (R %u/%d/%u G %u/%d/%u B %u/%d/%u)\n"
  56.         , R
  57.         , eCodes - preveCodes, eCodes
  58.         , eBits - preveBits, eBits
  59.         , abs(AbseRtotal - preveRtotal), eRtotal , AbseRtotal
  60.         , abs(AbseGtotal - preveGtotal), eGtotal , AbseGtotal
  61.         , abs(AbseBtotal - preveBtotal), eBtotal , AbseBtotal
  62.     );
  63.     preveRtotal = AbseRtotal;
  64.     preveGtotal = AbseGtotal;
  65.     preveBtotal = AbseBtotal;
  66.     preveCodes = eCodes;
  67.     preveBits = eBits;
  68.     }
  69.     printf("%u total codes\n", 256*256*256);
  70.     printf("total error: %u codes %u bits (R %d/%u G %d/%u B %d/%u)\n"
  71.     , eCodes
  72.     , eBits
  73.     , eRtotal , AbseRtotal
  74.     , eGtotal , AbseGtotal
  75.     , eBtotal , AbseBtotal
  76.     );
  77.     exit(0);
  78. }
  79.  
  80. float    *lumaRed;
  81. float    *lumaGreen;
  82. float    *lumaBlue;
  83.  
  84. static float*
  85. setupLuma(float c)
  86. {
  87.     float *v = (float *)malloc(256 * sizeof (float));
  88.     int i;
  89.     for (i = 0; i < 256; i++)
  90.     v[i] = c * i;
  91.     return (v);
  92. }
  93.  
  94. static void
  95. setupLumaTables()
  96. {
  97.     lumaRed = setupLuma(LumaRed);
  98.     lumaGreen = setupLuma(LumaGreen);
  99.     lumaBlue = setupLuma(LumaBlue);
  100. }
  101.  
  102. static unsigned
  103. V2Code(float f, unsigned long RB, unsigned long RW, int CR)
  104. {
  105.     unsigned int c = (unsigned int)((((f)*(RW-RB)/CR)+RB)+.5);
  106.     return (c > 255 ? 255 : c);
  107. }
  108.  
  109. #define    Code2V(c, RB, RW, CR)    ((((c)-(int)RB)*(float)CR)/(float)(RW-RB))
  110.  
  111. #define    CLAMP(f,min,max) \
  112.     (int)((f)+.5 < (min) ? (min) : (f)+.5 > (max) ? (max) : (f)+.5)
  113.  
  114. check(int R, int G, int B)
  115. {
  116.     float Y, Cb, Cr;
  117.     int iY, iCb, iCr;
  118.     float rY, rCb, rCr;
  119.     float rR, rG, rB;
  120.     int eR, eG, eB;
  121.  
  122.     Y = lumaRed[R] + lumaGreen[G] + lumaBlue[B];
  123.     Cb = (B - Y)*D2;
  124.     Cr = (R - Y)*D1;
  125.     iY = V2Code(Y, refBlackWhite[0], refBlackWhite[1], 255);
  126.     iCb = V2Code(Cb, refBlackWhite[2], refBlackWhite[3], 127);
  127.     iCr = V2Code(Cr, refBlackWhite[4], refBlackWhite[5], 127);
  128.     rCb = Code2V(iCb, refBlackWhite[2], refBlackWhite[3], 127);
  129.     rCr = Code2V(iCr, refBlackWhite[4], refBlackWhite[5], 127);
  130.     rY = Code2V(iY, refBlackWhite[0], refBlackWhite[1], 255);
  131.     rR = rY + rCr*D3;
  132.     rB = rY + rCb*D4;
  133.     rG = rY - rCb*D6 - rCr*D5;
  134.     eR = R - CLAMP(rR,0,255);
  135.     eG = G - CLAMP(rG,0,255);
  136.     eB = B - CLAMP(rB,0,255);
  137.     if (abs(eR) > 1 || abs(eG) > 1 || abs(eB) > 1) {
  138.     printf("R %u G %u B %u", R, G, B);
  139.     printf(" Y %g Cb %g Cr %g", Y, Cb, Cr);
  140.     printf(" iY %u iCb %u iCr %u", iY, iCb, iCr);
  141.     printf("\n -> Y %g Cb %g Cr %g", rY, rCb, rCr);
  142.     printf(" R %g (%u) G %g (%u) B %g (%u) E=[%d %d %d])\n"
  143.         , rR, CLAMP(rR,0,255)
  144.         , rG, CLAMP(rG,0,255)
  145.         , rB, CLAMP(rB,0,255)
  146.         , eR, eG, eB
  147.     );
  148.     }
  149.     eRtotal += eR;
  150.     eGtotal += eG;
  151.     eBtotal += eB;
  152.     AbseRtotal += abs(eR);
  153.     AbseGtotal += abs(eG);
  154.     AbseBtotal += abs(eB);
  155.     if (eR | eG | eB)
  156.     eCodes++;
  157.     eBits += abs(eR) + abs(eG) + abs(eB);
  158. }
  159.